package org.flashbrighton.as3bots.collision
{
	import org.flashbrighton.as3bots.geom.Metrics;
	import org.flashbrighton.as3bots.utils.NumberUtils;
	

	public class SweepTestMethods
	{
	
		/**
		 * test for whether mb is inside ma, and colliding (ie for the Arena)
		 */
		public static function innerCircle(ma:Metrics, mb:Metrics):Number
		{
			var x1:Number = ma.circle.x, 
				y1:Number = ma.circle.y,
				w1:Number = ma.circle.radius,
				dx1:Number = ma.vector.x, 
				dy1:Number = ma.vector.y, 
				
				x2:Number = mb.circle.x, 
				y2:Number = mb.circle.y,
				w2:Number = mb.circle.radius,
				dx2:Number = mb.vector.x, 
				dy2:Number = mb.vector.y,
								
				a:Number, 
				b:Number, 
				c:Number, 
				dist:Number, 
				t:Number;
			
			dist = w1-w2;
			
			a = NumberUtils.sqrd(dx2-dx1)+NumberUtils.sqrd(dy2-dy1);
			b = 2*(x1*(dx1-dx2)+x2*(dx2-dx1)+y1*(dy1-dy2)+y2*(dy2-dy1));
			c = NumberUtils.sqrd(x2-x1)+NumberUtils.sqrd(y2-y1)-NumberUtils.sqrd(dist);
			
			t = (-b+Math.sqrt((NumberUtils.sqrd(b))-(4*a*c)))/(2*a);
		
			if (t && t>0 && t<1) {
				
				return t;
			}else if (t && t>-1 && t<0) 
			{
				return 0;
			}
			else
			{
				return -1;
			}
	
			
			
		}
		
		/**
		 * test for whether mb is outside ma, and colliding
		 */
		public static function circleToCircle(ma:Metrics, mb:Metrics):Number
		{
			var x1:Number = ma.circle.x, 
				y1:Number = ma.circle.y,
				w1:Number = ma.circle.radius,
				dx1:Number = ma.vector.x, 
				dy1:Number = ma.vector.y, 
				
				x2:Number = mb.circle.x, 
				y2:Number = mb.circle.y,
				w2:Number = mb.circle.radius,
				dx2:Number = mb.vector.x, 
				dy2:Number = mb.vector.y,
								
				a:Number, 
				b:Number, 
				c:Number,
				dist:Number, 
				t:Number;
			
			dist = w1+w2;
			
			a = NumberUtils.sqrd(dx2-dx1)+NumberUtils.sqrd(dy2-dy1);
			b = 2*(x1*(dx1-dx2)+x2*(dx2-dx1)+y1*(dy1-dy2)+y2*(dy2-dy1));
			c = NumberUtils.sqrd(x2-x1)+NumberUtils.sqrd(y2-y1)-NumberUtils.sqrd(dist);
			
			t = (-b-Math.sqrt((NumberUtils.sqrd(b))-(4*a*c)))/(2*a);
			
			if (t && t>0 && t<1) {
				
				return t;
			}else if (t && t>-1 && t<0) 
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}

	}

	
}